<html><head><meta charset="utf-8"><title>09 Spring IoC容器ApplicationContext如何实现国际化-慕课专栏</title>
			<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
			<meta name="renderer" content="webkit">
			<meta property="qc:admins" content="77103107776157736375">
			<meta property="wb:webmaster" content="c4f857219bfae3cb">
			<meta http-equiv="Access-Control-Allow-Origin" content="*">
			<meta http-equiv="Cache-Control" content="no-transform ">
			<meta http-equiv="Cache-Control" content="no-siteapp">
			<link rel="apple-touch-icon" sizes="76x76" href="https://www.imooc.com/static/img/common/touch-icon-ipad.png">
			<link rel="apple-touch-icon" sizes="120x120" href="https://www.imooc.com/static/img/common/touch-icon-iphone-retina.png">
			<link rel="apple-touch-icon" sizes="152x152" href="https://www.imooc.com/static/img/common/touch-icon-ipad-retina.png">
			<link href="https://moco.imooc.com/captcha/style/captcha.min.css" rel="stylesheet">
			<link rel="stylesheet" href="https://www.imooc.com/static/moco/v1.0/dist/css/moco.min.css?t=201907021539" type="text/css">
			<link rel="stylesheet" href="https://www.imooc.com/static/lib/swiper/swiper-3.4.2.min.css?t=201907021539">
			<link rel="stylesheet" href="https://static.mukewang.com/static/css/??base.css,common/common-less.css?t=2.5,column/zhuanlanChapter-less.css?t=2.5,course/inc/course_tipoff-less.css?t=2.5?v=201907051055" type="text/css">
			<link charset="utf-8" rel="stylesheet" href="https://www.imooc.com/static/lib/ueditor/themes/imooc/css/ueditor.css?v=201907021539"><link rel="stylesheet" href="https://www.imooc.com/static/lib/baiduShare/api/css/share_style0_16.css?v=6aba13f0.css"></head>
			<body><div id="main">


<div class="main-con hide-menu">
    <!-- 左侧菜单 & 索引 -->
    
    <div class="right-content" style="padding-left: 0px;">
        <div class="container clearfix" id="top" style="width: 1134px; display: block;">
            
            
            <div class="center_con js-center_con l" style="width: 1134px;">
                <div class="article-con">
                                            <!-- 买过的阅读 -->
                        

                    
                    <div class="art-title" style="margin-top: 0px;">
                        09 Spring IoC容器ApplicationContext如何实现国际化
                    </div>
                    <div class="art-info clearfix">
                        
                        <span class="l">
                            更新时间：2020-08-10 14:43:24
                        </span>
                    </div>
                    <div class="art-top">
                                                <img src="https://img3.sycdn.imooc.com/5ece09a9000100e706400359.jpg" alt="">
                                                                        <div class="famous-word-box">
                            <img src="https://www.imooc.com/static/img/column/bg-l.png" alt="" class="bg1 bg">
                            <img src="https://www.imooc.com/static/img/column/bg-r.png" alt="" class="bg2 bg">
                            <div class="famous-word">人生太短，要干的事太多，我要争分夺秒。——爱迪生<p></p></div>
                        </div>
                                            </div>
                    <div class="art-content js-lookimg">
                        <div id="article_content">
                            <div class="cl-preview-section"><h2 id="背景" style="font-size: 30px;">背景</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">国际化是指让产品或是程序在无需做出改变的情况下，就能够适应不同语言和地区的需要， internationalization 单词太长 ，又被简称为 i18n（取头取尾中间有 18 个字母）。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">我们知道 ApplicationContext 实现了 MessageSource 接口来实现国家化 i18n 的，我们来看看吧！</p>
</div><div class="cl-preview-section"><h2 id="applicationcontext-是如何实现国际化的？" style="font-size: 30px;">ApplicationContext 是如何实现国际化的？</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">本章节建议先展示国际化的效果（通过国际化可以实现的功能），再通过配置代码去实现。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">在 Java 中实现国际化主要是借助一个工具类 ResourceBundle ，核心的思想就是 ，对不同的语言环境提供一个不同的资源文件 。Spring 提供了 ResourceBundleMessageSource 来封装 ResourceBundle 来完成国际化。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><strong>实例</strong></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">能动手就别仅仅动眼，尝试来做一个示例：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">1.在 Resources 目录下建立 locale 目录（名字任意取），并创建 messages 开头的不同 locale 的属性文件。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="http://p3.pstatp.com/large/pgc-image/b24375ced6c24f368eea72d3acd24d0f" data-original="http://p3.pstatp.com/large/pgc-image/b24375ced6c24f368eea72d3acd24d0f" alt="img"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">2.配置 ResourceBundleMessageSource 的 bean 使之生效。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">可以通过 XML 或者 annotation（注解） 来实现。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">XML 方式：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="http://p1.pstatp.com/large/pgc-image/ff088b0605514611bfa6c88bc682ff86" data-original="http://p1.pstatp.com/large/pgc-image/ff088b0605514611bfa6c88bc682ff86" alt="img"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">其中 basename 是指 locale 目录下 messages 开头的文件。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">注解方式：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="http://p3.pstatp.com/large/pgc-image/a171beace2384f8b8f50fb1ae55f5321" data-original="http://p3.pstatp.com/large/pgc-image/a171beace2384f8b8f50fb1ae55f5321" alt="img"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">测试程序</p>
</div><div class="cl-preview-section"><pre class="  language-java"><code class="prism  language-java"> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>  
     MessageSource messageSource<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ClassPathXmlApplicationContext</span><span class="token punctuation">(</span><span class="token string">"beans.xml"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  
     String username_us<span class="token operator">=</span>messageSource<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token string">"userName"</span><span class="token punctuation">,</span>null<span class="token punctuation">,</span>Locale<span class="token punctuation">.</span>US<span class="token punctuation">)</span><span class="token punctuation">;</span>  
     String username_chinese<span class="token operator">=</span>messageSource<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token string">"userName"</span><span class="token punctuation">,</span>null<span class="token punctuation">,</span>Locale<span class="token punctuation">.</span>CHINESE<span class="token punctuation">)</span><span class="token punctuation">;</span>  
     System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"chinese:"</span><span class="token operator">+</span>username_chinese<span class="token punctuation">)</span><span class="token punctuation">;</span>  
     System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"english:"</span><span class="token operator">+</span>username_us<span class="token punctuation">)</span><span class="token punctuation">;</span>  
 <span class="token punctuation">}</span>
</code></pre>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><strong>QA1：</strong></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">可能会有人说，上面的示例都是单个 properties 文件，如果一个 locale 下有多个属性文件，改如何做呢？</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">其实很简单，我们来看示例：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="http://p1.pstatp.com/large/pgc-image/233ec68193b0454ea9aac91749459a88" data-original="http://p1.pstatp.com/large/pgc-image/233ec68193b0454ea9aac91749459a88" alt="img"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><strong>QA2：</strong></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">还有人会问，你上面的获取指定 locale 的，每次都要带上 locale，支持不同 locale 看着很费劲，其实还可以使用全局变量，除了全家变量外还可以通过设置 jvm 参数，设置 locale，如下示例：</p>
<pre><code>set JAVA_ARGS=-Duser.language=fr -Duser.region=FR %JAVA_ARGS%
</code></pre>
</div><div class="cl-preview-section"><h2 id="工作原理" style="font-size: 30px;">工作原理</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">通过上面的程序一步步调试，可以看到：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="http://p1.pstatp.com/large/pgc-image/6d8bc30bb9e74f02960285d0024594be" data-original="http://p1.pstatp.com/large/pgc-image/6d8bc30bb9e74f02960285d0024594be" alt="img"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">我们来看看 Properties 文件要如何加载的？ResourceBundleMessageSource 的内部类 MessageSourceControl#newBundle() 方法：</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="https://img1.sycdn.imooc.com/5f28ddcf0001546b08860874.png" data-original="//img1.sycdn.imooc.com/5f28ddcf0001546b08860874.png" alt="图片描述"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">加载时如果可以找到响应的资源则进行加载，否则创建一个默认的 locale。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">第一步的加载使用 ResourceBundleMessageSource.java 的 loadBundle() 完成。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;"><img class="" src="http://p3.pstatp.com/large/pgc-image/a091b751105d4dceb45065089a743f08" data-original="http://p3.pstatp.com/large/pgc-image/a091b751105d4dceb45065089a743f08" alt="img"></p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">读取属性 Properties 资源有两种形式，一种使用 Reader，一种使用 InputStream 方式。</p>
</div><div class="cl-preview-section"><h2 id="总结：" style="font-size: 30px;">总结：</h2>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">spring applicationContext 国际化有三要素：<strong>ResourceBundle</strong>，<strong>MessageFormat</strong>，<strong>ResourceBundle.Control</strong>。其背后的原理是根据 locale 来加载响应 locale 的属性文件。</p>
</div><div class="cl-preview-section"><p style="font-size: 20px; line-height: 38px;">根据 locale 加载属性文件遵循 RFC-4647 规则，查找是根据最优匹配原则，先语言后地区。故如果一个语言仅仅在一个地区有使用的话或者多个地区使用的语言习惯一致的话，只要标识语言就可以了；如果一个语言在多个地区使用，且使用的语言习惯有迥异，如汉字中的简体字和繁体字，那么就需要标明语言和地区了。</p>
</div>}
                        </div>
                    </div>
                                            <!-- 买过的阅读 -->
                        <div class="art-next-prev clearfix">
                                                                                                <!-- 已买且开放 或者可以试读 -->
                                    <a href="/read/77/article/2021">
                                                                    <div class="prev l clearfix">
                                        <div class="icon l">
                                            <i class="imv2-arrow3_l"></i>
                                        </div>
                                        <p>
                                            08 青出于蓝而胜于蓝之揭秘Spring容器ApplicationContext
                                        </p>
                                    </div>
                                </a>
                                                                                                                            <!-- 已买且开放 或者可以试读 -->
                                    <a href="/read/77/article/2023">
                                                                    <div class="next r clearfix">
                                        <p>
                                            10 扩展之Spring MVC中如何实现国际化i18n
                                        </p>
                                        <div class="icon r">
                                            <i class="imv2-arrow3_r"></i>
                                        </div>

                                    </div>
                                </a>
                                                    </div>
                                    </div>
                <div class="comments-con js-comments-con" id="coments_con">
                </div>

                
            </div>
            
            
            

        </div>
    </div>
</div>

<div class="modal modal-jiaQun-new hide" id="modal-jiaQun">
    <div class="inner" style="">
        <div class="modal-close js-close-jiaQun">
            <i class="imv2-close"></i>
        </div>
        <div class="content">
            <img src="https://img2.sycdn.imooc.com/5f1a80010001c9a105340522.jpg">
            <div class="right-info">
                <div class="title">
                    扫码加入慕课Java核心用户群
                </div>
                <div class="desc">
                                            <p class="mb6">验证信息：<span id="joincode">2011161431052393</span><span class="copy js-copy-joincode">复制</span></p>
                                        <p class="mb6">QQ讨论群号：314316732</p>
                                            <p>QQ群URL：<a href="https://jq.qq.com/?_wv=1027&amp;k=OouwHZGZ" target="_blank">点击访问</a></p>
                                    </div>
            </div>
            <p class="tip">若遇到搜索不到QQ群或加群失败，请联系客服邮箱:kf@imooc.com</p>
        </div>
    </div>
</div>
 
<!-- 专栏介绍页专栏评价 -->

<!-- 专栏介绍页底部三条评价 -->

<!-- 专栏阅读页弹层目录和介绍页页面目录 -->

<!-- 专栏阅读页发布回复 -->

<!-- 专栏阅读页发布评论 -->

<!-- 专栏阅读页底部评论 -->

<!-- 专栏阅读 单个 评论 -->

<!-- 新增回复和展开三条以外回复 -->

<!-- 立即订阅的弹窗 -->












</div></body></html>